package com.vf.admin.client.dao;

import com.vf.admin.client.dto.AppRoomDto;
import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppFollow;
import com.vf.admin.miaodong.entity.AppRoom;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 房间
 */
@Repository
public class AppRoomDao extends BaseDao<AppRoom> {
    /**
     * 查询列表
     */
    public List<AppRoom> list(Map<String, Object> paraMap, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_room where 1=1  ");
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppRoom.class, splitPage);
    }


    /**
     * 获取批量房间获取关注信息
     * @param arr 房间id 拼接
     * @return
     */
    public List<AppRoom> getPiliang(String arr) {

        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_room where 1=1  ");
        sbStr.append(" and id in ( ");
        sbStr.append(arr);
        sbStr.append(" ) ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppRoom.class);
    }


    /**
     * 详情
     */
    public AppRoom get(Integer id) {
        if (StringEx.isNullOrLing(id)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_room where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppRoom.class);
    }

    /**
     * 详情
     */
    public AppRoom getUserId(Integer userid) {
        if (StringEx.isNullOrLing(userid)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_room where 1=1");
        sbStr.append(" and userid = ? ");
        paramList.add(userid);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppRoom.class);
    }


    /**
     * 详情
     */
    public AppRoom getRoomCode(String roomcode) {
        if (StringEx.isNullOrLing(roomcode)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_room where 1=1");
        sbStr.append(" and roomcode = ? ");
        paramList.add(roomcode);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppRoom.class);
    }

    /**
     * 详情
     */
    public AppRoom getTxCode(Integer txcode) {
        if (StringEx.isNullOrLing(txcode)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_room where 1=1");
        sbStr.append(" and tengxuncode = ? ");
        paramList.add(txcode);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppRoom.class);
    }


    /**
     * 根据类型&分类获取房间
     *
     * @param splitPageBean  分业
     * @param xingzhi 房间性质
     * @return
     * @xzx
     */
    public List<AppRoom> getTypeRoomList(Integer xingzhi,SplitPageBean splitPageBean) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_room where kbzhuangtai=1");
        if(xingzhi != null) {
            sbStr.append(" and xingzhi = ? ");
            paramList.add(xingzhi);
        }
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppRoom.class,splitPageBean);
    }

 /**
     * 获取正常的全部房间
     *
     * @return
     * @xzx
     */
    public List<AppRoom> getRoomAll() {

        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_room where 1=1 and status=1 and kbzhuangtai=1");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppRoom.class,null);
    }

    public List<AppRoom> getRoomFiveMinute() {

        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_room where 1=1 and status=1 and kbzhuangtai=1 and createtime < date_add(now(),interval - 30 second)");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppRoom.class,null);
    }


    /**
     * 添加/修改
     */
    public void save(AppRoom haoYou) {
        if (haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 删除
     */
    public void del(Integer id) {
        if (id <= 0) {
            return;
        }
        String sql = "delete  from app_room where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }

    /**
     * 后天管理获取所有房间
     *
     * @param map
     * @param bean
     */
    public List<AppRoom> findList(Map<String, Object> map, SplitPageBean bean) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append(" SELECT a.* FROM app_room a where 1=1 ");
        if (StringEx.isNotNull(map.get("nick"))) {
            sbStr.append(" and a.nick like ? ");
            paramList.add("%" + map.get("nick") + "%");
        }
        if (StringEx.isNotNull(map.get("roomname"))) {
            sbStr.append(" and a.roomname like ? ");
            paramList.add("%" + map.get("roomname") + "%");
        }
        if (StringEx.isNotNull(map.get("phone"))) {
            sbStr.append(" and a.phone = ? ");
            paramList.add(map.get("phone"));
        }
        if (StringEx.isNotNull(map.get("roomcode"))) {
            sbStr.append(" and a.roomcode = ? ");
            paramList.add(map.get("roomcode"));
        }
        if (StringEx.isNotNull(map.get("status"))) {
            sbStr.append(" and a.status = ? ");
            paramList.add(map.get("status"));
        }
        if (!StringEx.isNull(map.get("startDate"))) {
            sbStr.append(" and a.createtime>=? ");
            paramList.add(map.get("startDate"));
        }
        if (!StringEx.isNull(map.get("endDate"))) {
            sbStr.append(" and a.createtime<=? ");
            paramList.add(map.get("endDate"));
        }
        sbStr.append(" order by a.createtime desc ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppRoom.class, bean);
    }

    /**
     * 根据code查询房间
     *
     * @param roomcode
     * @param splitPage
     * @return
     */
    public List<AppRoom> selectRoomByCode(String roomcode, SplitPageBean splitPage) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("select * from app_room where 1=1");
        if (!StringUtils.isEmpty(roomcode)) {
            sbStr.append(" AND roomcode=?");
            paramList.add(roomcode);
        }
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppRoom.class, splitPage);
    }


    /**
     * 查询推荐的视频房间
     *
     * @return
     */
    public List<AppRoom> splist() {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_room where roomstatus = 1  ");
        sbStr.append(" order by onlinenum desc");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppRoom.class);
    }


    public List<AppRoomDto> getRoomByCat(Integer catid, Integer type, SplitPageBean page) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("select t.name,r.createtime,r.roomcode,r.roomtypeid,r.roompass,r.passstate,r.roomname,r.roomimg,r.roomnotice,r.shi" +
                ",r.onlinenum,r.userid,r.pic,r.nick,r.id" +
                " from app_room as r left join app_roomtype as t on t.id = r.roomtypeid where r.roomtypeid = ? and r.status = 1 and r.xingzhi = ? " +
                "order by (IFNULL((SELECT SUM(gr.money) FROM app_gift_reward as gr where gr.roomid = r.roomcode AND YEARWEEK(date_format(gr.createtime,'%Y-%m-%d')) = YEARWEEK(now())),0) ) DESC");
        paramList.add(catid);
        paramList.add(type);

        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppRoomDto.class, page);
    }

    public List<AppRoom> roomTuiJianList(Integer type, SplitPageBean page) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("select * from app_room roomstatus = 1 and xingzhi= ? ");
        paramList.add(type);
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppRoom.class, page);
    }

    public List<AppRoom> getSearch(String sousuo,SplitPageBean page){
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("select * from app_room where xingzhi= 2 and ( roomcode like ? or roomname like ? ) ");
        paramList.add("%"+sousuo+"%");
        paramList.add("%"+sousuo+"%");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppRoom.class, page);
    }

    public AppRoom selectByUserId(Integer userid) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("select * from app_room where userid = ?");
        paramList.add(userid);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppRoom.class);
    }


    /**
     * 更新用户头像
     *
     * @param userid
     * @param pic    头像地址
     */
    public void updateImg(Integer userid, String pic) {
        if (StringEx.isNull(userid)) {
            return;
        }
        if (StringEx.isNull(pic)) {
            return;
        }
        String sql = " UPDATE app_room SET pic = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    /**
     * 更新用户昵称
     *
     * @param userid
     * @param nick   昵称
     */
    public void updateNick(Integer userid, String nick) {
        if (StringEx.isNull(userid)) {
            return;
        }
        if (StringEx.isNull(nick)) {
            return;
        }
        String sql = " UPDATE app_room SET nick = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    /**
     * @Description 获取房间信息
     * @Param: [id]
     * @return: com.vf.admin.miaodong.entity.AppRoom
     * @Author: ljx
     * @Date: 2021/07/29
     */
    public AppRoom getById(Integer id) {
        if (StringEx.isNull(id)) {
            return null;
        }
        return this.get(id, AppRoom.class);
    }


    /**
     * 更换房间封面
     *
     * @Param: [roomid 房间id, img 封面]
     * @return: void
     * @Author: zhouyunyi
     * @Date: 2021/07/2
     * @Time: 15:05
     */
    public void uproomimg(Integer roomid, String img) {
        if (StringEx.isNull(roomid)) {
            return;
        }
        if (StringEx.isNull(img)) {
            return;
        }
        String sql = " UPDATE app_room SET roomimg = ?  WHERE id = ?  ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(img);
        paramList.add(roomid);
        this.update(sql, paramList);
    }

    /**
     * 更改房间标签类型
     *
     * @Param: [roomid 房间id, roomtypeid 类型id, roomtypename 类型名称, xingzhi 性质]
     * @return: void
     * @Author: zhouyunyi
     * @Date: 2021/07/2
     * @Time: 15:48
     */
    public void uproomtype(Integer roomid, Integer roomtypeid, String roomtypename, Integer xingzhi) {
        if (StringEx.isNull(roomid)) {
            return;
        }
        if (StringEx.isNull(roomtypeid)) {
            return;
        }
        if (StringEx.isNull(roomtypename)) {
            return;
        }
        if (StringEx.isNull(xingzhi)) {
            return;
        }
        String sql = " UPDATE app_room SET roomtypeid = ? ,roomtypename = ? ,xingzhi = ? WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(roomtypeid);
        paramList.add(roomtypename);
        paramList.add(xingzhi);
        paramList.add(roomid);
        this.update(sql, paramList);
    }

    /**
     * 更改房间标题
     *
     * @Param: [roomid 房间id, roomname 房间名称]
     * @return: void
     * @Author: zhouyunyi
     * @Date: 2021/07/2
     * @Time: 15:58
     */
    public void uproomname(Integer roomid, String roomname) {
        if (StringEx.isNull(roomid)) {
            return;
        }
        if (StringEx.isNull(roomname)) {
            return;
        }
        String sql = " UPDATE app_room SET roomname = ?  WHERE  id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(roomname);
        paramList.add(roomid);
        this.update(sql, paramList);
    }

    /**
     * 更换房间背景图
     * @Param: [roomid 房间id, roombeijing 背景图, roombeijingid 背景图id]
     * @return: void
     * @Author: zhouyunyi
     * @Date: 2021/07/3
     * @Time: 10:57
     */
    public void uproombeijing(Integer roomid,String roombeijing,Integer roombeijingid){
        if(StringEx.isNull(roomid)){
            return;
        }
        if(StringEx.isNull(roombeijing)){
            return;
        }
        if(StringEx.isNull(roombeijingid)){
            return;
        }
        String sql = " UPDATE app_room SET roombeiimg = ? ,roombeiimgid = ? WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(roombeijing);
        paramList.add(roombeijingid);
        paramList.add(roomid);
        this.update(sql,paramList);
    }

    /**
     * @Description 礼物特效开关
     * @Param: [roomid 房间id, leixing 1开启  2关闭]
     * @return: void
     * @Author: ljx
     * @Date: 2021/07/3
     */
    public void updateliwutexiao(Integer roomid, Integer leixing) {

        if (StringEx.isNull(roomid)) {
            return;
        }
        if (StringEx.isNull(leixing)) {
            return;
        }
        String sql = " UPDATE app_room SET gifteffects = ?  WHERE  id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(leixing);
        paramList.add(roomid);
        this.update(sql, paramList);

    }

    /**
     * @Description 进房提示开关
     * @Param: [roomid 房间id, leixing 1开启  2关闭]
     * @return: void
     * @Author: ljx
     * @Date: 2021/07/3
     */
    public void updatejinfangtishi(Integer roomid, Integer leixing) {

        if (StringEx.isNull(roomid)) {
            return;
        }
        if (StringEx.isNull(leixing)) {
            return;
        }
        String sql = " UPDATE app_room SET intoroomstate = ?  WHERE  id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(leixing);
        paramList.add(roomid);
        this.update(sql, paramList);

    }


    /**
     * @Description 更新用户开关和密码
     * @Param: [roomid, leixing, pass]
     * @return: void
     * @Author: ljx
     * @Date: 2021/07/3
     */
    public void updateroomkaiguan(Integer roomid, Integer leixing, String pass) {

        if (StringEx.isNull(roomid)) {
            return;
        }
        if (StringEx.isNull(leixing)) {
            return;
        }
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder str = new StringBuilder("");
        str.append("UPDATE app_room SET passstate = ? ");
        paramList.add(leixing);
        if (StringEx.isNotNull(pass)) {
            str.append(",roompass = ? ");
            paramList.add(pass);
        }
        str.append(" WHERE id = ? ");
        paramList.add(roomid);
        String sql = str.toString();
        this.update(sql, paramList);

    }

    /**
     * @Description 更新房间公告
     * @Param: [roomid, gonggao]
     * @return: void
     * @Author: ljx
     * @Date: 2021/07/3
     */
    public void updateroomgonggao(Integer roomid, String gonggao) {

        if (StringEx.isNull(roomid)) {
            return;
        }
        if (StringEx.isNull(gonggao)) {
            return;
        }
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder str = new StringBuilder("");
        str.append("UPDATE app_room SET roomnotice = ? ");
        str.append(" WHERE id = ? ");
        paramList.add(gonggao);
        paramList.add(roomid);
        String sql = str.toString();
        this.update(sql, paramList);
    }

    /**
     * @Description 更新房间直播中
     * @Param: [roomid, gonggao]
     * @return: void
     * @Author: xzx
     * @Date: 2021/07/3
     */
    public void updateRoomZhiboState(Integer roomid, Integer zaixian) {

        if (StringEx.isNull(roomid)) {
            return;
        }
        if (StringEx.isNull(zaixian)) {
            return;
        }
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder str = new StringBuilder("");
        str.append("UPDATE app_room SET kbzhuangtai = ? ");
        str.append(" WHERE id = ? ");
        paramList.add(zaixian);
        paramList.add(roomid);
        String sql = str.toString();
        this.update(sql, paramList);
    }

    /**
     * @Description 更新房间 市
     * @Param: [roomid, gonggao]
     * @return: void
     * @Author: ljx
     * @Date: 2021/07/3
     */
    public void updateroomshi(Integer roomid, String shi) {

        if (StringEx.isNull(roomid)) {
            return;
        }
        if (StringEx.isNull(shi)) {
            return;
        }
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder str = new StringBuilder("");
        str.append("UPDATE app_room SET shi = ? ");
        str.append(" WHERE id = ? ");
        paramList.add(shi);
        paramList.add(roomid);
        String sql = str.toString();
        this.update(sql, paramList);
    }

    public List<AppRoom> getByNearbyUser(String lon, String lat, SplitPageBean splitPageBean){
        if (StringEx.isNull(lon) || StringEx.isNull(lon)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("SELECT *,(st_distance (POINT (lon,lat),POINT( ? , ? ) ) * 111195  ) AS mi FROM app_room WHERE 1=1 and lon IS NOT NULL and nearfunction = 1 ORDER BY mi asc,onlinestatus asc ");
        paramList.add(lon);
        paramList.add(lat);
        String sql = sbStr.toString();

        return this.queryList(sql, paramList, AppRoom.class, splitPageBean);
    }
}
